import 'package:eh_mobile/common/utils/extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import '../../smart_home_info/model/device_dynamic_model.dart';
import '../common/home_page_common_utils.dart';
import '../controller.dart';
import 'device_dynamic_button.dart';

class HomeDynamicView extends StatefulWidget {
  HomepageController pageController;

  HomeDynamicView({required this.pageController, super.key});

  @override
  State<HomeDynamicView> createState() => _HomeDynamicViewState();
}

class _HomeDynamicViewState extends State<HomeDynamicView> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      // height: 66.h,
      child: RepaintBoundary(
        child: SingleChildScrollView(
          controller: widget.pageController.dynamicController,
          padding: EdgeInsets.fromLTRB(12.w, 16.h, 12.w, 12.h),
          scrollDirection: Axis.horizontal,
          child: Row(
            children: _buildTopDeviceDynamicItems(),
          ),
        ),
      ),
    );
  }

  // 创建顶部设备动态列表
  List<Widget> _buildTopDeviceDynamicItems() {
    final items = <Widget>[];
    for (var i = 0; i < widget.pageController.deviceDynamicList.length; i++) {
      DeviceDynamicModelData deviceDynamicMap = widget.pageController.deviceDynamicList[i];
      DeviveDynamicButton deviceTypeButton = DeviveDynamicButton(
          icon: SvgPicture.asset(
            HomePageCommonUtils.getRightIconName(deviceDynamicMap.type ?? ''),
            colorFilter:
                const ColorFilter.mode(Color(0xffFFD638), BlendMode.srcIn),
            theme: const SvgTheme(currentColor: Color(0xffFFEB87)),
          ),
          title: HomePageCommonUtils.getRightDeviceName(deviceDynamicMap.type ?? ''),
          content: HomePageCommonUtils.getRightDeviceInfo(deviceDynamicMap),
          index: i,
          onPressed: (index) {
            widget.pageController.deviceDynamicAction(index);
          });
      items.add(deviceTypeButton);
      if (i < widget.pageController.deviceDynamicList.length - 1) {
        items.add(13.sizedBoxWidth);
      }
    }
    return items;
  }
}
